<script setup>
import {
    Message,
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { reactive } from 'vue'
import { ref } from 'vue'

//对话框
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'

const form = reactive({
  name: '',
  region: '',
  date1: '',
  date2: '',
  delivery: false,
  type: [],
  resource: '',
  desc: '',
})


//商品列表数据模型
const articles = ref([
])


//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(5)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    articleList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num
    articleList()
}

//回显商品分类
import {articleListService,articleAddService,articleUpdateService,articleDeleteService} from '@/api/article.js'
const origin= ref('')
const fruitName= ref('')
//获取商品列表数据
const articleList=async()=>{
    let params={
        pageNum:pageNum.value,
        pageSize:pageSize.value,
      origin: origin.value?origin.value:"",
        fruitName: fruitName.value?fruitName.value:"",

    }
  let result = await articleListService(params);
  //渲染视图
  total.value=result.data.total;
  articles.value=result.data.items;

//数据处理
for(let i=0; i<articles.value.length;i++){
    let article=articles.value[i];
    if(article.status==0){
        article.statusName="私密";
    }else{
        article.statusName="公开";
    }
}


}
articleList();

//抽屉组件
import {Plus} from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const articleModel = ref({
    "fruitName":'',
    "coverImg":'',
    "origin": '',
    "weight":'',
    "productFeatures":'',
    "price":'',
    "merchantId":'',
    "merchantNickname":'',
    "createTime":'',
    "updateTime":'',
    "status":''
})

//导入token
import {useTokenStore} from '@/stores/token.js';
const tokenStore=useTokenStore();

//上传成功的回调函数
const uploadSuccess=(result)=>{
    articleModel.value.coverImg=result.data;
    console.log(result.data);
}

//查看产品详情页
const look=(row)=>{
    articleModel.value.fruitName=row.fruitName;
    articleModel.value.coverImg=row.coverImg;
    articleModel.value.origin=row.origin;
    articleModel.value.weight=row.weight;
    articleModel.value.productFeatures=row.productFeatures;
    articleModel.value.price=row.price;
    articleModel.value.merchantId=row.merchantId;
    articleModel.value.merchantNickname=row.merchantNickname;
    articleModel.value.createTime=row.createTime;
    articleModel.value.updateTime=row.updateTime;
    articleModel.value.status=row.status;
//扩展id属性
articleModel.value.id=row.id;

}

//添加商品
import {ElMessage, localeContextKey} from 'element-plus'
const addArticle=async(clickState)=>{
    //把发布状态赋值给数据模型
    articleModel.value.status=clickState;
    articleModel.value.origin=origin.value;

    //调用接口
    let result=await articleAddService(articleModel.value);

    ElMessage.success(result.msg?result.msg:'添加成功');

    //让抽屉消失
    visibleDrawer.value=false;
    origin.value='';fruitName.value='';
    //刷新当前列表
 
    articleList()

}
//定义变量控制标题的展示
const title=ref('')

//商品修改
const showDrawer=(row)=>{
    visibleDrawer.value=true;
    title.value='编辑商品';
    //数据拷贝
    articleModel.value.fruitName=row.fruitName;
    articleModel.value.coverImg=row.coverImg;
    articleModel.value.origin=row.origin;
    origin.value=articleModel.value.origin;
    articleModel.value.weight=row.weight;
    articleModel.value.productFeatures=row.productFeatures;
    articleModel.value.price=row.price;
    articleModel.value.merchantId=row.merchantId;
    articleModel.value.merchantNickname=row.merchantNickname;
    articleModel.value.createTime=row.createTime;
    articleModel.value.updateTime=row.updateTime;
    articleModel.value.status=row.status;
//扩展id属性
articleModel.value.id=row.id;
}
//编辑商品*******************************************************
const updateArticle=async(clickState)=>{
    articleModel.value.status=clickState;
    articleModel.value.origin=origin.value;
    //调用接口
    let result=await articleUpdateService(articleModel.value);
    ElMessage.success('修改成功')
    

   //让抽屉消失
   visibleDrawer.value=false;
    //刷新当前列表
    origin.value='';fruitName.value='';
    articleList()

}
//清空模型的数据
const clearData=()=>{
    articleModel.value.fruitName='';
    articleModel.value.coverImg='';
    articleModel.value.origin='';
    articleModel.value.weight='';
    articleModel.value.productFeatures='';
    articleModel.value.price='';
    articleModel.value.merchantId='';
    articleModel.value.merchantNickname='';
    articleModel.value.createTime='';
    articleModel.value.updateTime='';
    articleModel.value.status='';
}
 
//删除商品
import {ElMessageBox} from 'element-plus'
const deleteArticle=(row)=>{
    //提示用户  确认框
    ElMessageBox.confirm(
    '您确认要删除商品吗？',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async() => {
//调用接口
let result=await articleDeleteService(row.id);

      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      //刷新列表
      origin.value='';fruitName.value='';
      articleList();
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '用户取消了删除',
      })
    })

}




//选中
let multipleSelection=ref([])
const selectedItems=ref([])
const handleSelectionChange=(val)=>{
    /* dialogFormVisible=val
    console(val);*/
/* console.log("handleSelectionChange",val)
selectedItems.value=val */
multipleSelection=[]
val.forEach(row=> {
    multipleSelection.push(row.id)
    
})
console.log(multipleSelection);


} 

//批量删除
const handleDelList=()=>{
    multipleSelection.forEach(id=>{
        deleteArticle({id})
    })
    multipleSelection=[]

}




</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>商品信息</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer=true;title='添加商品';clearData();origin=''">添加商品</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="产地：">
                <el-select placeholder="请选择产地" v-model="origin">
                    <el-option label="中国河北省" value="中国河北省"></el-option>  
<el-option label="中国山西省" value="中国山西省"></el-option>  
<el-option label="中国辽宁省" value="中国辽宁省"></el-option>  
<el-option label="中国吉林省" value="中国吉林省"></el-option>  
<el-option label="中国黑龙江省" value="中国黑龙江省"></el-option>  
<el-option label="中国江苏省" value="中国江苏省"></el-option>  
<el-option label="中国浙江省" value="中国浙江省"></el-option>  
<el-option label="中国安徽省" value="中国安徽省"></el-option>  
<el-option label="中国福建省" value="中国福建省"></el-option>  
<el-option label="中国江西省" value="中国江西省"></el-option>  
<el-option label="中国山东省" value="中国山东省"></el-option>  
<el-option label="中国河南省" value="中国河南省"></el-option>  
<el-option label="中国湖北省" value="中国湖北省"></el-option>  
<el-option label="中国湖南省" value="中国湖南省"></el-option>  
<el-option label="中国广东省" value="中国广东省"></el-option>  
<el-option label="中国海南省" value="中国海南省"></el-option>  
<el-option label="中国四川省" value="中国四川省"></el-option>  
<el-option label="中国贵州省" value="中国贵州省"></el-option>  
<el-option label="中国云南省" value="中国云南省"></el-option>  
<el-option label="中国陕西省" value="中国陕西省"></el-option>  
<el-option label="中国甘肃省" value="中国甘肃省"></el-option>  
<el-option label="中国青海省" value="中国青海省"></el-option>  
<el-option label="中国台湾省" value="中国台湾省"></el-option>  
<el-option label="中国内蒙古自治区" value="中国内蒙古自治区"></el-option>  
<el-option label="中国广西壮族自治区" value="中国广西壮族自治区"></el-option>  
<el-option label="中国西藏自治区" value="中国西藏自治区"></el-option>  
<el-option label="中国宁夏回族自治区" value="中国宁夏回族自治区"></el-option>  
<el-option label="中国新疆维吾尔自治区" value="中国新疆维吾尔自治区"></el-option>  
<el-option label="中国北京市" value="中国北京市"></el-option>  
<el-option label="中国天津市" value="中国天津市"></el-option>  
<el-option label="中国上海市" value="中国上海市"></el-option>  
<el-option label="中国重庆市" value="中国重庆市"></el-option>  
<el-option label="中国香港特别行政区" value="中国香港特别行政区"></el-option>  
<el-option label="中国澳门特别行政区" value="中国澳门特别行政区"></el-option>
                  
                </el-select>
            </el-form-item>

            <el-form-item label="商品名称:" prop="fruitName">
                    <el-input v-model="fruitName" placeholder="请输入商品名称"></el-input>
                </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="articleList">搜索</el-button>
                <el-button @click="origin='';fruitName=''">重置</el-button>
                <el-button type="danger" @click="handleDelList">批量删除</el-button>
            </el-form-item>
        </el-form>







        <!-- 商品列表 -->
        <el-table 
        ref="multipleTableRef"
        :data="articles" 
        style="width: 100%"
        @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="编号" width="100" prop="id"></el-table-column>
            <el-table-column label="名称" prop="fruitName"></el-table-column>
            <el-table-column label="产地" prop="origin"> </el-table-column>
            <el-table-column label="重量" prop="weight"></el-table-column>
            <el-table-column label="价格" prop="price"></el-table-column>
            <el-table-column label="状态" prop="statusName"></el-table-column>
            <el-table-column label="操作" width="250">
                <template #default="{ row }">
                    <el-button text @click="dialogFormVisible = true;look(row)">查看</el-button>
                    <el-button :icon="Edit" circle plain type="primary" @click="showDrawer(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteArticle(row)"></el-button>


                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5 ,10, 15]"
            layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
            @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
   
   <!-- 抽屉组件 -->
   <!-- 抽屉 -->
   <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
            <!-- 添加文章表单 -->
            <el-form :model="articleModel" label-width="100px" >
                <el-form-item label="产品名称" prop="fruitName">
                    <el-input v-model="articleModel.fruitName" placeholder="请输入产品名称"></el-input>
                </el-form-item>

               

                <el-form-item label="商品图片" prop="coverImg">
<!-- 
    auto-upload:设置是否自动上传
    action:设置服务器接口路径
    name:设置上传的文件字段名
    headers：设置上传的请求头
    on-success：设置上传成功的回调函数
 -->
                    <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false"
                    action="/api/upload"
                    name="file"
                    :headers="{'Authorization':tokenStore.token}"
                    :on-success="uploadSuccess"
                    >
                        <img v-if="articleModel.coverImg" :src="articleModel.coverImg" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>


                <el-form-item label="产地：">
                <el-select placeholder="请选择产地" v-model="origin">
                    <el-option label="中国河北省" value="中国河北省"></el-option>  
<el-option label="中国山西省" value="中国山西省"></el-option>  
<el-option label="中国辽宁省" value="中国辽宁省"></el-option>  
<el-option label="中国吉林省" value="中国吉林省"></el-option>  
<el-option label="中国黑龙江省" value="中国黑龙江省"></el-option>  
<el-option label="中国江苏省" value="中国江苏省"></el-option>  
<el-option label="中国浙江省" value="中国浙江省"></el-option>  
<el-option label="中国安徽省" value="中国安徽省"></el-option>  
<el-option label="中国福建省" value="中国福建省"></el-option>  
<el-option label="中国江西省" value="中国江西省"></el-option>  
<el-option label="中国山东省" value="中国山东省"></el-option>  
<el-option label="中国河南省" value="中国河南省"></el-option>  
<el-option label="中国湖北省" value="中国湖北省"></el-option>  
<el-option label="中国湖南省" value="中国湖南省"></el-option>  
<el-option label="中国广东省" value="中国广东省"></el-option>  
<el-option label="中国海南省" value="中国海南省"></el-option>  
<el-option label="中国四川省" value="中国四川省"></el-option>  
<el-option label="中国贵州省" value="中国贵州省"></el-option>  
<el-option label="中国云南省" value="中国云南省"></el-option>  
<el-option label="中国陕西省" value="中国陕西省"></el-option>  
<el-option label="中国甘肃省" value="中国甘肃省"></el-option>  
<el-option label="中国青海省" value="中国青海省"></el-option>  
<el-option label="中国台湾省" value="中国台湾省"></el-option>  
<el-option label="中国内蒙古自治区" value="中国内蒙古自治区"></el-option>  
<el-option label="中国广西壮族自治区" value="中国广西壮族自治区"></el-option>  
<el-option label="中国西藏自治区" value="中国西藏自治区"></el-option>  
<el-option label="中国宁夏回族自治区" value="中国宁夏回族自治区"></el-option>  
<el-option label="中国新疆维吾尔自治区" value="中国新疆维吾尔自治区"></el-option>  
<el-option label="中国北京市" value="中国北京市"></el-option>  
<el-option label="中国天津市" value="中国天津市"></el-option>  
<el-option label="中国上海市" value="中国上海市"></el-option>  
<el-option label="中国重庆市" value="中国重庆市"></el-option>  
<el-option label="中国香港特别行政区" value="中国香港特别行政区"></el-option>  
<el-option label="中国澳门特别行政区" value="中国澳门特别行政区"></el-option>
                </el-select>
            </el-form-item>
                <!-- <el-form-item label="产地" prop="origin">
                    <el-input v-model="articleModel.origin" placeholder="请输入产品产地"></el-input>
                </el-form-item> -->

                <el-form-item label="重量" prop="weight">
                    <el-input v-model="articleModel.weight" placeholder="请输入产品重量"></el-input>
                </el-form-item>



                <el-form-item label="产品描述" prop="productFeatures">
                    <div class="editor">
                        <quill-editor theme="snow" v-model:content="articleModel.productFeatures" contentType="html"></quill-editor>
                    </div>
                </el-form-item>




                <el-form-item label="价格" prop="price">
                    <el-input v-model="articleModel.price" placeholder="请输入产品价格"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="title=='添加商品'?addArticle('1'):updateArticle('1')">公开</el-button>
                    <el-button type="info" @click="title=='添加商品'?addArticle('0'):updateArticle('0')">私密</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>




<!-- 对话框 -->
<el-dialog v-model="dialogFormVisible" title="产品详情页:">
    <el-form :model="form">
        <el-form-item label="产品名称:" prop="fruitName">
            <span>{{ articleModel.fruitName }}</span>
                </el-form-item>
      
                <el-form-item label="商品图片:" prop="coverImg">
<!-- 
    auto-upload:设置是否自动上传
    action:设置服务器接口路径
    name:设置上传的文件字段名
    headers：设置上传的请求头
    on-success：设置上传成功的回调函数
 -->
                    <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false"
                    action="/api/upload"
                    name="file"
                    :headers="{'Authorization':tokenStore.token}"
                    :on-success="uploadSuccess"
                    >
                        <img v-if="articleModel.coverImg" :src="articleModel.coverImg" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>

               <el-form-item label="规格:" prop="weight">
                <span>{{ articleModel.weight }}</span>
                </el-form-item>

                <el-form-item label="单价:" prop="price">
                    <span>{{ articleModel.price }}</span>
                </el-form-item>

                <el-form-item label="产品特色:" prop="productFeatures">
                    <span>{{ articleModel.productFeatures }}</span>
                </el-form-item>

                <el-form-item label="发布时间:" prop="createTime">
                    <span>{{ articleModel.createTime }}</span>
                </el-form-item>

                <el-form-item label="当前商家:" prop="merchantId">
                    <span>{{ articleModel.merchantNickname }}</span>
                </el-form-item>

    </el-form>
  </el-dialog>


               

   
   
   </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
/* 抽屉样式 */
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}

//对话框
.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}



</style>